# coding=utf-8
import os
import sys

# urllib库不兼容Python2和Python3，对2和3版本进行区分
from queue import Queue

from pxr import Usd, UsdGeom, Gf

from Analyzer.USD_Analyzer import USD_Analyzer
from MongoDBUtils.MongoDB_Utils import MyMongoDBUtils

if sys.version[0] == '2':
    pass
elif sys.version[0] == '3':
    pass

import pymongo

from MongoDBConfig.MongoDB_Config import MyMongoDBConfig
from MongoDBUtils.MongoDB_Utils import MyMongoDBUtils



"""
判断存储内容是否为中文Chinese
"""
def isChinese(ustring):
    for item in ustring:
        try:
            if u'\u4e00' <= item <= u'\u9fff':
                return True
        except UnicodeDecodeError:
            return True
    return False




# MongoDB服务类
class MyMongoDBBaseService():
    def __init__(self, MongoDBURL, MongoDBName):
        # 解析url
        host, port = MyMongoDBConfig.analysisUrl(MongoDBURL)
        self.client = pymongo.MongoClient(host = host, port = int(port, 10))
        self.db = self.client[MongoDBName]

        # collection定义
        collectionName_set = 'set'
        collectionName_asset = 'assets'
        collectionName_dic = 'dictionary'


        set_fileroot = 'Property/Kitchen_set2'
        asset_fileroot = 'Property/Kitchen_set2/assets'     # 注意路径不是../Property...！！！不用向上跳一级，main.py调用,要看main函数相对路径
        usd_fileroot = 'Property/Kitchen_set2/Kitchen_set_instanced.usd'

        # 向MongoDB插入两个set的USD文件
        # 向MongoDB插入所有assets的USD文件
        # 解析USD结构并生成场景树，以字典形式存储进MongoDB


        # self.insertTwoSets(collectionName_set, set_fileroot)
        # self.insertAllAssets(collectionName_asset, asset_fileroot)
        # self.buildDic(collectionName_dic, usd_fileroot)



        new_fileroot = 'Resource'
        set_filename = 'Kitchen_set'
        set_instanced_filename = 'Kitchen_set_instanced'


        # 从MongoDB数据库中获取“Kitchen_set.usd”和“Kitchen_set_instanced.usd”
        # 从MongoDB数据库中获取所有assets


        new_assetfileroot = 'Resource/Kitchen_set2'
        # self.obtainTwoSets(collectionName_set, new_fileroot, set_filename, set_instanced_filename)
        # self.obtainAllAssets(collectionName_asset, new_assetfileroot)






        """
        ----------------------------------------测试 1----------------------------------------
        只获取场景中的杯子类
        """
        # assetName = self.getAssetNamebyPrimName(collectionName_dic, 'CupCBlue_1')
        # self.getUSDbyAssetName(collectionName_asset, new_fileroot, assetName)
        """
        ----------------------------------------测试 1----------------------------------------
        """




        """
        ----------------------------------------测试 2----------------------------------------
        将冰箱上的盒子进行变体修改，从CerealBoxB ——> CerealBoxA
        """
        # self.selectCurVariant(collectionName_dic, 'CerealBoxB_1', "modelingVariant", "CerealBoxB")
        # self.insertTwoSets(collectionName_set, set_fileroot)
        # self.insertAllAssets(collectionName_asset, asset_fileroot)
        # self.obtainTwoSets(collectionName_set, new_fileroot, set_filename, set_instanced_filename)
        # primNameList = self.getAllPrimChildName(collectionName_dic, 'FridgeArea_grp')
        # print(primNameList)
        # for primName in primNameList:
        #     print(primName)
        #     assetName = self.getAssetNamebyPrimName(collectionName_dic, primName)
        #     self.getUSDbyAssetName(collectionName_asset, new_fileroot, assetName)
        """
        ----------------------------------------测试 2----------------------------------------
        """



        """
        ----------------------------------------测试 3----------------------------------------
        找到某一个3D坐标点在场景中哪个prim上
        """
        # rootPrimName = 'Kitchen_set'
        # PointPos = (0.0, 0.0, 0.0) # (x, y, z)
        # MinPoint = (-1.0 , -1.0 , -1.0)
        # MaxPoint = (1.0 , 1.0 , 1.0)
        # primName = self.findOnePointinBox(collectionName_dic, PointPos, rootPrimName)
        # for item in primName:
        #     assetName = self.getAssetNamebyPrimName(collectionName_dic, item)
        #     self.getUSDbyAssetName(collectionName_asset, new_fileroot, assetName)
        """
        ----------------------------------------测试 3----------------------------------------
        """



        """
        ----------------------------------------测试 4----------------------------------------
        返回某一个指定prim的属性列表及其值
        """
        # attributeList = self.getAttributeList(collectionName_dic, "HangerLightBrown_1")
        # for item in attributeList:
        #     print(item + "的值：")
        #     self.getAttributeValue(collectionName_dic, "HangerLightBrown_1", item)
        # self.getVariantList(collectionName_dic, "HangerLightBrown_1", "shadingVariant")
        # self.getCurVariantSelection(collectionName_dic, "HangerLightBrown_1", "shadingVariant")
        """
        ----------------------------------------测试 4----------------------------------------
        """





    '''
    指定一个数据库
    '''
    def setmongoDB(self, newMongoDBName):
        self.db = self.client[newMongoDBName]



    '''
    指定一个集合collection
    '''
    def setCollection(self, collectionName):
        return self.db[collectionName]



    """
    添加一个prim
    """
    def addPrim(self, collectionName, primName, content):
        collection = self.setCollection(collectionName)
        # 检查是否有该名称的prim
        condition = {'name': primName}
        result = collection.find_one(condition)
        content_childList = content[0]     # 注意这里不能写成str!!!，否则存进库里的就成了一个字符串，没法读取
        content_prim_path = str(content[1])
        content_bbox = content[2]
        content_asset_name = content[3]
        content_attributeList = content[4]
        content_variantListDic = content[5]
        content = [content_childList, content_prim_path, content_bbox, content_asset_name, content_attributeList, content_variantListDic]
        # 如果该prim的name查不到
        if not result:
            data = {
                'name': primName,
                'content': content
            }
            ret = collection.insert_one(data)
            return ret
        return None



    """
    根据name查找一个prim
    """
    def getPrim(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        # 如果找到该primName
        if result:
            ret = collection.find_one(result)
            return ret
        return None



    """
    更新一个prim的name
    """
    def updatePrim(self, collectionName, primName, newPrimName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        # 如果查不到
        if not result:
            return False
        else :
            update = {'$set': {'name': newPrimName}}
            ret = collection.update_one(result, update)
            return ret



    """
    根据name删除一个prim
    """
    def deletePrim(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        if not result:
            return False
        else :
            collection.delete_one(condition)



    """
    获取指定prim下的childList列表
    """
    def getchildList(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        # 如果找到该primName
        if result:
            ret = collection.find_one(result)
            # 获取键值对primName（第一条）的value
            midData = list(ret.values())[2]
            childList = midData[0]
            # print(childList)
            return childList
        return None


    """
    判断指定prim是否拥有childList
    """
    def haveChildList(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        # 如果找到该primName
        if result:
            ret = collection.find_one(result)
            midData = list(ret.values())[2]
            childList = midData[0]
            if childList != None:
                # print(childList)
                return True
            return False
        else:
            return False



    """
    获取指定USD文件下的content
    """
    def getContent(self, collectionName, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        # 如果找到该USDName
        if result:
            ret = collection.find_one(result)
            content = ret.values()
            print(content)
            return content
        return None



    """
    判断指定USD文件是否拥有content，即是否是具有content二进制内容文件的usd文件
    """
    def haveContent(self, collectionName, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        # 如果找到该USDName
        if result:
            ret = collection.find_one(result)
            content = ret.values()
            # print(contentList)
            if content != None:
                return True
            return False
        else :
            return False



    """
    添加一个USD文件
    """
    def addUSDFile(self, collectionName, prefile, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if not result:
            # USDName是usd文件名称
            filename = prefile + '/' + USDName + '/' + USDName
            filename_usd = filename + '.usd'
            filename_payload_usd = filename + '_payload.usd'
            filename_geom_usd = filename + '.geom.usd'

            content_usd = MyMongoDBUtils.getBinFile(filename_usd)
            content_payload_usd = MyMongoDBUtils.getBinFile(filename_payload_usd)
            content_geom_usd = MyMongoDBUtils.getBinFile(filename_geom_usd)
            content = [content_usd, content_payload_usd, content_geom_usd]
            data = {
                'name' : USDName,
                'content' : content  # content中有content_usd, content_payload_usd, content_geom_usd三个文件
            }
            collection.insert_one(data)
        if result:
            # USDName是usd文件名称
            filename = prefile + '/' + USDName + '/' + USDName
            filename_usd = filename + '.usd'
            filename_payload_usd = filename + '_payload.usd'
            filename_geom_usd = filename + '.geom.usd'

            content_usd = MyMongoDBUtils.getBinFile(filename_usd)
            content_payload_usd = MyMongoDBUtils.getBinFile(filename_payload_usd)
            content_geom_usd = MyMongoDBUtils.getBinFile(filename_geom_usd)
            content = [content_usd, content_payload_usd, content_geom_usd]
            update = {'$set': {'content': content}}
            collection.update_one(result, update)



    """
    向数据库中导入assets文件夹下的所有usd文件
    """
    def addAllAssets(self, collectionName, fileroot):
        files = os.listdir(fileroot)

        # 向每个assets下的文件夹下插入三种usd文件
        for file in files:
            prefile, extension = os.path.splitext(os.path.basename(file))
            # print(file_name)
            self.addUSDFile(collectionName, fileroot, prefile)



    """
    向数据库中导入kitchen_set
    """
    def addSet(self, collectionName, fileroot, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if not result:
            # USDName = 'Kitchen_set'
            # 向根路径下插入实例化和未实例化的set.usd文件
            filename = fileroot + '/' + USDName
            filename_usd = filename + '.usd'

            content = MyMongoDBUtils.getBinFile(filename_usd)
            data = {
                'name' : USDName,
                'content' : content  # content中有content_usd, content_payload_usd, content_geom_usd三个文件
            }
            collection.insert_one(data)
        if result:
            # USDName = 'Kitchen_set'
            # 向根路径下插入实例化和未实例化的set.usd文件
            filename = fileroot + '/' + USDName
            filename_usd = filename + '.usd'

            content = MyMongoDBUtils.getBinFile(filename_usd)
            update = {'$set': {'content': content}}
            collection.update_one(result, update)



    """
    向数据库中提取assets文件夹下的所有usd文件并新建到resource中
    """
    def getAllAssets(self, collectionName, newfileroot):
        collection = self.setCollection(collectionName)
        data = collection.find()
        path = newfileroot + '/' + 'assets'
        MyMongoDBUtils.mkdir('assets')
        for i in data:
            content = i.get('content')
            usd = content[0]
            pay_load_usd = content[1]
            geom_usd = content[2]
            name = i.get('name')
            filename = path + '/' + name
            MyMongoDBUtils.mkdir(filename)
            name_usd = name
            name_pay_load_usd = name + '_payload'
            name_geom = name + '.geom'
            MyMongoDBUtils.create_usd(filename, name_usd, usd)
            MyMongoDBUtils.create_usd(filename, name_pay_load_usd, pay_load_usd)
            MyMongoDBUtils.create_usd(filename, name_geom, geom_usd)



    """
    向数据库中提取Kitchen_set及其实例化文件并新建到resource中
    """
    def getSet(self, collectionName, newfileroot, filename):
        collection = self.setCollection(collectionName)
        path = newfileroot + '/' + 'Kitchen_set2'
        MyMongoDBUtils.mkdir(path)
        condition = {'name': filename}
        data = collection.find_one(condition)
        name = data.get('name')
        content = data.get('content')
        name_usd = name
        MyMongoDBUtils.create_usd(path, name_usd, content)



    """
    按照一个prim的name查找获得其assetName
    """
    def getAssetNamebyPrimName(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        # 如果找到该primName
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            assetName = content[2][3]
            # print(assetName)
            return assetName
        return None



    """
    按照一个已知的assetName（USDName）查找获得其usd文件
    """
    def getUSDbyAssetName(self, collectionName, newfileroot, USDName):
        collection = self.setCollection(collectionName)
        path = newfileroot + '/' + 'Kitchen_set2' + '/' + 'assets'
        condition = {'name': USDName}
        result = collection.find_one(condition)
        # 如果找到该assetName
        if result:
            data = collection.find_one(result)
            name = data.get('name')
            content = data.get('content')
            usd = content[0]
            pay_load_usd = content[1]
            geom_usd = content[2]
            filename = path + '/' + name
            MyMongoDBUtils.mkdir(filename)
            name_usd = name
            name_pay_load_usd = name + '_payload'
            name_geom = name + '.geom'
            MyMongoDBUtils.create_usd(filename, name_usd, usd)
            MyMongoDBUtils.create_usd(filename, name_pay_load_usd, pay_load_usd)
            MyMongoDBUtils.create_usd(filename, name_geom, geom_usd)



    """
    按照一个prim的name查找获得其USD文件
    """
    def getUSDbyPrimName(self, collectionName, newfileroot, primName):
        assetName = self.getAssetNamebyPrimName(collectionName, primName)
        self.getUSDbyAssetName(collectionName, newfileroot, assetName)



    """
    获得一个prim节点下所有的叶子节点名称
    """
    def getAllPrimChildName(self, collectionName, primName):
        collection = self.setCollection(collectionName)
        condition = {'name': primName}
        result = collection.find_one(condition)
        primNameList = []
        # 如果找到该primName
        if result:
            data = collection.find_one(result)
            # 如果该节点为叶子节点
            if self.haveChildList(collectionName, primName) == False:
                name = data.get('name')
                primNameList.append(name)
                return primNameList
            else:
                name = data.get('name')
                # print(name)
                # queue放入当前层prim节点的名字
                queue = Queue()
                queue.put(primName)

                # 当队列不为空时
                while (queue.qsize() > 0):
                    # 每次while循环当前prim节点名字
                    curPrimName = queue.get()
                    # 如果当前prim节点可再分
                    if self.haveChildList(collectionName, curPrimName):
                        if self.getchildList(collectionName, curPrimName) != None:
                            childPrimNameList = self.getchildList(collectionName, curPrimName)
                            # print(childPrimNameList)
                            # print(type(childPrimNameList))
                            for itemName in childPrimNameList:
                                queue.put(itemName)
                                if self.getchildList(collectionName, itemName) == None:
                                    primNameList.append(itemName)
                                else:
                                    continue
                    else:
                        continue
                # print(primNameList)
                return primNameList




    """
    向MongoDB插入两个set的USD文件
    """
    def insertTwoSets(self, collectionName_set, set_fileroot):
        self.addSet(collectionName_set, set_fileroot, 'Kitchen_set')
        self.addSet(collectionName_set, set_fileroot, 'Kitchen_set_instanced')



    """
    向MongoDB插入所有assets的USD文件
    """
    def insertAllAssets(self, collectionName_asset, asset_fileroot):
        self.addAllAssets(collectionName_asset, asset_fileroot)



    """
    从MongoDB获取两个set的USD文件
    """
    def obtainTwoSets(self, collectionName_set, new_fileroot, set_filename, set_instanced_filename):
        self.getSet(collectionName_set, new_fileroot, set_filename)
        self.getSet(collectionName_set, new_fileroot, set_instanced_filename)



    """
    从MongoDB获取所有assets的USD文件
    """
    def obtainAllAssets(self, collectionName_asset, new_assetfileroot):
        self.getAllAssets(collectionName_asset, new_assetfileroot)



    """
    解析USD结构并生成场景树，以字典形式存储进MongoDB
    """
    def buildDic(self, collectionName_dic, usd_fileroot):
        analyzer = USD_Analyzer()
        dic = analyzer.buildDic(usd_fileroot)
        for key in dic.keys():
            primName = key
            content = dic[key]
            self.addPrim(collectionName_dic, primName, content)



    """
    从数据库中获取某个叶子prim的全部属性及其变换操作顺序
    """
    def getAttributes(self, collectionName, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            attributes = content[2][4]
            print(attributes)
            return attributes
        else:
            return None



    """
    从数据库中获取某个叶子prim的属性变换操作列表
    """
    def getAttributeList(self, collectionName, USDName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            attributes = content[2][4]
            attributeList = attributes['xformOpOrder']
            print("该prim的所有属性变换操作列表attributeList为：")
            print(attributeList)
            # print(type(attributeList))
            return attributeList
        else:
            return None



    """
    从数据库中获取某个叶子prim的某个指定属性的值
    注意分类讨论类型！！
    """
    def getAttributeValue(self, collectionName, USDName, attributeName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            attributes = content[2][4]
            attributeValue = attributes[attributeName]

            # xformOp:translate，平移变换，类型：tuple
            if attributeName == 'xformOp:translate':
                attributeValue = tuple(attributeValue)

            # xformOp:rotateXYZ，绕xyz轴旋转，类型：tuple
            if attributeName == 'xformOp:rotateXYZ':
                attributeValue = tuple(attributeValue)

            # xformOp:scale，尺寸缩放，类型：tuple
            if attributeName == 'xformOp:scale':
                attributeValue = tuple(attributeValue)

            print(attributeValue)
            # print(type(attributeValue))

            return attributeValue
        else:
            return None



    """
    从数据库中获取某个叶子prim某个变体类型的当前变体
    """
    def getCurVariantSelection(self, collectionName, USDName, variantSetName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            variantListDic = content[2][5]
            for item in variantListDic:
                variantListDicVariantSetName = item['variantSetName']
                if variantListDicVariantSetName == variantSetName:
                    curVariantSelection = item['curVariantSelection']
                    print("当前选择的变体为：")
                    print(curVariantSelection)
                    return curVariantSelection
        else:
            return None



    """
    从数据库中获取某个叶子prim某个变体类型的所有可选变体
    """
    def getVariantList(self, collectionName, USDName, variantSetName):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName}
        result = collection.find_one(condition)
        if result:
            ret = collection.find_one(result)
            content = list(ret.values())
            variantListDic = content[2][5]
            # print(variantListDic)
            for item in variantListDic:
                variantListDicVariantSetName = item['variantSetName']
                # print(variantListDicVariantSetName)
                if variantListDicVariantSetName == variantSetName:
                    variantList = item['variantList']
                    print("所有可选变体：")
                    print(variantList)
                    return variantList
                else:
                    continue
        else:
            return None


    """
    从数据库更改当前变体
    """
    def selectCurVariant(self, collectionName, USDName, variantSetName, curVariantSelection):
        collection = self.setCollection(collectionName)
        condition = {'name': USDName, 'content.5.0.variantSetName': variantSetName}
        result = collection.find_one(condition)
        # 找到对应的条目
        if result:
            # 判断curVariantSelection是否在其variantList内
            variantList = self.getVariantList(collectionName, USDName, variantSetName)
            print(variantList)
            if curVariantSelection in variantList:
                update = {'$set': {'content.5.0.curVariantSelection': curVariantSelection}}
                collection.update_one(condition, update)
                # 更新模型文件
                # 打开usd文件
                usd_file = 'Property/Kitchen_set2/Kitchen_set_instanced.usd'
                stage = Usd.Stage.Open(usd_file)
                prim_path = list(result.values())[2][1]
                curPrim = stage.GetPrimAtPath(prim_path)
                # print(curPrim)
                if curPrim:
                    # 获取当前变体属性
                    variant_set = curPrim.GetVariantSet(variantSetName)
                    # 设置当前变体
                    variant_set.SetVariantSelection(curVariantSelection)
                    # 写入 USD 文件
                    stage.Save()
                    # print(curPrim.GetVariantSet(variantSetName))
                    print("当前prim资产" + USDName + "的变体类型" + variantSetName + "，当前变体选择已更改为" + curVariantSelection + "!")
            else:
                print("当前prim不具有该变体！")
                return False
        elif result == None:
            condition = {'name': USDName, 'content.5.1.variantSetName': variantSetName}
            result = collection.find_one(condition)
            print(result)
            if result:
                # 判断curVariantSelection是否在其variantList内
                variantList = self.getVariantList(collectionName, USDName, variantSetName)
                print(variantList)
                if curVariantSelection in variantList:
                    update = {'$set': {'content.5.1.curVariantSelection': curVariantSelection}}
                    collection.update_one(condition, update)
                    # 更新模型文件
                    # 打开usd文件
                    usd_file = 'Property/Kitchen_set2/Kitchen_set_instanced.usd'
                    stage = Usd.Stage.Open(usd_file)
                    prim_path = list(result.values())[2][1]
                    curPrim = stage.GetPrimAtPath(prim_path)
                    # print(curPrim)
                    if curPrim:
                        # 获取当前变体属性
                        variant_set = curPrim.GetVariantSet(variantSetName)
                        # 设置当前变体
                        variant_set.SetVariantSelection(curVariantSelection)
                        # 写入 USD 文件
                        stage.Save()
                        # print(curPrim.GetVariantSet(variantSetName))
                        print(
                            "当前prim资产" + USDName + "的变体类型" + variantSetName + "，当前变体选择已更改为" + curVariantSelection + "!")
                else:
                    print("当前prim不具有该变体！")
                    return False
            else:
                return False
        else:
            return False



    """
    通过树状结构bbox快速查找某一个点包含的资源
    rootPrimName = 'Kitchen_set'
    """
    def findOnePointinBox(self, collectionName, PointPos, rootPrimName):
        collection = self.setCollection(collectionName)
        # 传入某个Point的(x,y,z)坐标
        posX = PointPos[0]
        posY = PointPos[1]
        posZ = PointPos[2]
        # 中间节点prim的name的队列
        midQueue = Queue()
        midQueue.put(rootPrimName)
        # 需要返回的相应实例孩子name列表resultList
        resultList = []

        while(midQueue.qsize() > 0):
            curPrimName = midQueue.get()
            condition = {'name': curPrimName}
            data = collection.find_one(condition)
            # 可再分
            if MyMongoDBBaseService.haveChildList(self, collectionName, curPrimName):
                childList = MyMongoDBBaseService.getchildList(self, collectionName, curPrimName)
                # 获得childList的长度
                childListLength = len(childList)
                # content第3条为bboxRange
                midData = list(data.values())[2]
                PointsList = midData[2]

                # bfs遍历bboxList
                for i in range(0, childListLength):
                    item = PointsList[i]
                    minPoint = list(item.values())[0]
                    maxPoint = list(item.values())[1]
                    # print("minPoint:" + str(minPoint))
                    # print("maxPoint:" + str(maxPoint))
                    # 如果该PointPos点位在bbox中
                    if minPoint[0] < posX < maxPoint[0] and \
                        minPoint[1] < posY < maxPoint[1] and \
                        minPoint[2] < posZ < maxPoint[2] :
                        midQueue.put(childList[i])

            # 已实例化
            else:
                # content第3条为bboxRange
                midData = list(data.values())[2]
                Points = midData[2]
                minPoint = list(Points.values())[0]
                maxPoint = list(Points.values())[1]
                # 如果该PointPos点位在bbox中
                if curPrimName == 'Kitchen_1':
                    continue
                if minPoint[0] < posX < maxPoint[0] and \
                        minPoint[1] < posY < maxPoint[1] and \
                        minPoint[2] < posZ < maxPoint[2]:
                    resultList.append(curPrimName)

        print("resultList:")
        print(resultList)
        return resultList



    """
    通过树状结构bbox快速查找某一范围内点覆盖包含的资源
    rootPrimName = 'Kitchen_set'
    """
    def findRangeinBox(self, collectionName, rootPrimName, min_point, max_point):
        collection = self.setCollection(collectionName)
        # 创建一个规定范围的包围盒
        ViewBbox = self.createBboxRange(min_point, max_point)
        # 中间节点prim的name的队列
        midQueue = Queue()
        midQueue.put(rootPrimName)
        # 需要返回的相应实例孩子name列表resultList
        resultList = []

        while(midQueue.qsize() > 0):
            curPrimName = midQueue.get()
            condition = {'name': curPrimName}
            data = collection.find_one(condition)
            # 可再分
            if MyMongoDBBaseService.haveChildList(self, collectionName, curPrimName):
                childList = MyMongoDBBaseService.getchildList(self, collectionName, curPrimName)
                # 获得childList的长度
                childListLength = len(childList)
                # content第3条为bboxRange
                midData = list(data.values())[2]
                PointsList = midData[2]

                # bfs遍历bboxList
                for i in range(0, childListLength):
                    item = PointsList[i]
                    minPoint = list(item.values())[0]
                    maxPoint = list(item.values())[1]
                    # print("minPoint:" + str(minPoint))
                    # print("maxPoint:" + str(maxPoint))
                    childPrimBboxRange = self.createBboxRange(minPoint, maxPoint)
                    print(ViewBbox)
                    # 如果ViewBbox和childPrimBboxRange相交
                    temp = ViewBbox
                    if ViewBbox.IntersectWith(childPrimBboxRange):
                        ViewBbox = temp
                        midQueue.put(childList[i])

            # 已实例化
            else:
                # content第3条为bboxRange
                midData = list(data.values())[2]
                Points = midData[2]
                minPoint = list(Points.values())[0]
                maxPoint = list(Points.values())[1]
                primBboxRange = self.createBboxRange(minPoint, maxPoint)
                # 如果cubeBbox和primBbox相交
                temp = ViewBbox
                if ViewBbox.IntersectWith(primBboxRange):
                    ViewBbox = temp
                    resultList.append(curPrimName)

        # print("resultList:")
        # print(resultList)
        return resultList


    """
    创建使用min_point和max_point创建一个范围
    """
    def createBboxRange(self, min_point, max_point):
        bbox_range = Gf.Range3d(Gf.Vec3d(*min_point), Gf.Vec3d(*max_point))

        return bbox_range